﻿module Runner {
    export class BattleOLD extends Phaser.Sprite {
        sprite:Phaser.Sprite;
        filter:Phaser.Filter;
        hero: Runner.Hero;
        enemies: Runner.Enemy[];
        combatWindow: Phaser.Graphics;
        combatLog: string[];
        timer:number = 0;
        cText: Phaser.Text;
        question: boolean;
        questionAsked: boolean = false;
        level: Level;

        constructor(game:Phaser.Game,hero: Runner.Hero, enemies: Runner.Enemy[], level: Level) {
            super(game, 0, 0);

            this.hero = new Hero(game, 0, 0, false);
            this.enemies = [new Enemy(game, 0, 0, enemies[0].key.toString(), null, null, null, false)];
            this.level = level;
            this.level.battleConclusion = 0;
            this.create();
        }

        create() {
            //  From http://glslsandbox.com/e#16153.0
            var fragmentSrc = [
                "precision mediump float;",

                "uniform float     time;",
                "uniform vec2      resolution;",
                "uniform vec2      mouse;",

                "#define MAX_ITER 4",

                "void main( void )",
                "{",
                "vec2 v_texCoord = gl_FragCoord.xy / resolution;",

                "vec2 p =  v_texCoord * 8.0 - vec2(20.0);",
                "vec2 i = p;",
                "float c = 1.0;",
                "float inten = .05;",

                "for (int n = 0; n < MAX_ITER; n++)",
                "{",
                "float t = time * (1.0 - (3.0 / float(n+1)));",

                "i = p + vec2(cos(t - i.x) + sin(t + i.y),",
                "sin(t - i.y) + cos(t + i.x));",

                "c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),",
                "p.y / (cos(i.y+t)/inten)));",
                "}",

                "c /= float(MAX_ITER);",
                "c = 1.5 - sqrt(c);",

                "vec4 texColor = vec4(0.039, 0.00, 0.072, 1.0);",

                "texColor.rgb *= (1.0 / (1.0 - (c + 0.05)));",

                "gl_FragColor = texColor;",
                "}"
            ];

            this.filter = new Phaser.Filter(this.level.game, null, fragmentSrc);
            this.filter.setResolution(GAMEWIDTH, GAMEHEIGHT);

            this.filters = [this.filter];
            this.width = GAMEWIDTH;
            this.height = GAMEHEIGHT;

            /*
            this.sprite = this.level.game.add.sprite(0,0);
            this.sprite.width = GAMEWIDTH;
            this.sprite.height = GAMEHEIGHT;

            this.sprite.filters = [this.filter];
            */

            var screenSubset = 1.4;
            var enemySpacing = (this.level.game.width / screenSubset) / (this.enemies.length + 1);

            for (var e = 0; e < this.enemies.length; e++) {
                //this.enemies[e].bringToTop();
                
                this.enemies[e].scale.x = this.enemies[e].scale.y = 2;
                this.enemies[e].x = (enemySpacing * (e + 1)) + (this.level.game.width - (this.level.game.width / screenSubset));
                this.enemies[e].y = this.level.game.height / 3;
                this.enemies[e].animations.play('shoot_backward');
                this.enemies[e].scale.x *= -1;
                this.addChild(this.enemies[e]);

                buildHitPointBar(this.enemies[e], this.level);

                this.enemies[e].events.onKilled.add(function (element) {
                    var index = this.enemies.indexOf(element);
                    this.enemies.splice(index, 1);

                    element.body = null;
                    element.destroy();

                    if (this.enemies.length <= 0) {
                        this.concludeBattle(1);
                    }
                }, this);
            }
            //this.hero.bringToTop();
            
            this.hero.scale.x = this.hero.scale.y = 2;
            this.hero.x = this.level.game.width - (this.level.game.width / 1.2);
            this.hero.y = this.level.game.height * 0.75;

            this.hero.animations.stop();
            this.hero.animations.frame = 0;
            this.addChild(this.hero);

            buildHitPointBar(this.hero, this.level);

            this.buildCombatLog();
        }

        update() {
            if (this.level.battleConclusion == 0) {
                this.filter.update(this.level.game.input.activePointer);
                if (!(this.enemies.length <= 0)) {
                    if (this.level.game.time.now > this.timer || this.question === undefined && this.questionAsked == false) {
                        if (this.question) {
                            this.enemies.forEach(function (element) {
                                if (element.alive) {
                                    var atkStrength = attack(this.hero.atkPower);
                                    this.writeCombatText('You hit ' + element.key + ' for: ' + atkStrength);
                                    Runner.removeHitPoints(element, atkStrength);

                                    if (dodge(this.hero.dodgeChance)) {
                                        this.writeCombatText('You dodge an attack!');
                                    } else {
                                        atkStrength = attack(element.atkPower);
                                        this.writeCombatText('You were hit for: ' + atkStrength);
                                        Runner.removeHitPoints(this.hero, atkStrength);
                                    }
                                }
                            }, this);
                            this.question = false;
                            this.questionAsked = false;
                            this.timer = this.level.game.time.now + 1000;
                        } else {
                            this.poseQuestion();
                        }
                    }
                }
            }
        }

        concludeBattle(conclusion: number = -1) {
            this.level.battleConclusion = conclusion;
        }

        destroy() {
            this.filters = null;
            this.filter = null;
            this.destroy();
            //this.sprite = null;
            this.enemies = null;
            this.combatWindow = null;
            this.combatLog = null;
            this.timer = null;
            this.cText = null;
        }


        poseQuestion() {
            var width = 400;
            var height = 100;
            this.question = false;
            if (this.questionAsked == false) {
                var question = Runner.askQuestion(4, 5, Runner.equationType.addition);
                question[0] = question[0] + ' = ';

                var correctAnswer: boolean = false;

                var questionWindow = this.level.game.add.graphics((this.level.game.width / 2) - width / 2, (this.level.game.height / 2) - height / 2);
                questionWindow.lineStyle(2, 0x000000, 0.4);
                questionWindow.beginFill(0xffffff, 0.2);
                questionWindow.drawRect(0, 0, width, height);
                questionWindow.endFill();

                var text = this.level.game.make.text(5, 5, question[0], { font: '30px verdana', fill: '#cccccc' });
                questionWindow.addChild(text);

                this.level.game.input.keyboard.addCallbacks(this, null, null, function (character) {
                    this.checkAnswer(character, questionWindow, text, question);
                });
            }
            this.questionAsked = true;
        }

        checkAnswer(character, questionWindow: Phaser.Graphics, questionText: Phaser.Text, question:any[]) :boolean {
            if (!isNaN(character)) {
                questionText.setText(question[0] + character);
                if (character == parseInt(question[1])) {
                    questionText.destroy();
                    questionWindow.destroy();
                    this.question = true;
                    return true;
                }
            }
            return false;
        }
    }
}